package com.gange.service.impl;

import com.gange.aspect.annotation.Log;
import com.gange.aspect.annotation.LogAnnotation;
import com.gange.mapper.EmpMapper;
import com.gange.pojo.Emp;
import com.gange.pojo.PageBean;
import com.gange.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper em;

    @LogAnnotation
    @Override
    @Log
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        System.out.println(this);
        PageHelper.startPage(page,pageSize);
        Page<Emp> pageInfo = em.page(name,gender,begin,end);
        long total = pageInfo.getTotal();
        List<Emp> result = pageInfo.getResult();
        return new PageBean(total,result);
    }

    @Override
    @Log
    public int deletes(List<Integer> ids) {
        return em.deletes(ids);
    }
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    @Log
    public void save(Emp emp) {
        //补全数据
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        //调用添加方法
        em.insert(emp);
    }
    @Override
    @Log
    public Emp login(Emp emp) {
        //调用dao层功能：登录
        Emp loginEmp = em.getByUsernameAndPassword(emp);

        //返回查询结果给Controller
        return loginEmp;
    }

}
